<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/5/22
 * Time: 17:29
 */

class Order extends M_Controller
{
    //创建订单
    public function createOrder(){
        $course_id = $this->input->get_post('course_id');
        $total_price = $this->input->get_post('total_price');
        $count = $this->input->get_post('count');
        $data = $this->db->where("id",$course_id)->get("public_class")->row_array();
        if(empty($data)){
            $this->msg("该课程不存在或者已经删除");
        }
        if($data['course_price']*$count != $total_price){
            $this->msg("价格不对");
        }
        if(empty(get_cookie('user_id'))){
            $this->msg("用户未登陆或者已经失效,请重新登陆",dr_user_url(APP_DIR.'/'.'login/index'));
        }
        $user = $this->db->where("id",get_cookie('user_id'))->get("user")->row_array();
        if(empty($user)){
            $this->msg("用户不存在或者已经删除");
        }
        //生成订单
        $order = array();
        $order['mobile'] = $user['mobile'];
        $order['name'] = $user['name'];
        //生成订单逻辑
        $order_no = time().rand(100000,999999);
        $order['order_no'] = $order_no;
        $order['addtime'] = time();
        $order['totalprice'] = $total_price;
        $order['source'] = 'pc下单';
        $order['course_name'] = $data['title'];
        $order['course_url'] = $data['cover'];
        //print_r($data);print_r($order);exit;
        $this->db->insert("course_order",$order);
        $order_id = $this->db->insert_id();
        $course = array();
        if($order_id){
            $course['stage'] = $data['grade'];
            $course['subject'] = $data['subject'];
            $course['type'] = $data['course_type'];
            $course['price'] = $data['course_price'];
            $course['num'] = $count;
            $course['totalprice'] = $total_price;
            $course['order_id'] = $order_id;
            $result = $this->db->insert("order_course",$course);
            if($result){
                $this->msg("添加订单成功",dr_user_url('pay/myorder'));
            }
        }
    }

    //修改订单状态
    public function modifyOrderStatus(){
        $sign = $this->input->get_post('sign');
        $order_no = $this->input->get_post('order_no');
        $pay_type = $this->input->get_post('pay_type');
        if(!empty($sign) && $sign == 'SD45645sdfmlsdfjusd12132snxnzchaqllqaqwqweqewq45dvs'){
            $paytype = 0;
            if($pay_type == 'zfb'){
                $paytype = 1;
            }else if($pay_type == 'wx'){
                $paytype = 2;
            }else if($pay_type == 'kfq'){
                $paytype = 3;
            }
            $this->db->where('order_no',$order_no)->update('course_order',array('pay_type'=>$paytype,'status'=>1));
        }
    }
    //获取订单状态
    public function orderStatus(){
        $oid  = $this->input->get_post('oid');
        $order = $this->db->select('status')->where('id',$oid)->get('course_order')->row_array();
        $arr = array();
        if(!empty($order)){
            if($order['status']){
                $arr['status'] = 1;
                $arr['msg'] = '订单已经支付成功,3秒后即将跳转';
            }else{
                $arr['status'] = 0;
                $arr['msg'] = '订单未支付';
            }
            echo json_encode($arr);exit;
        }else{
            $arr['status'] = -1;
            $arr['msg'] = '订单不存在';
        }
        echo json_encode($arr);exit;
    }

}